﻿namespace Collaboris.Saf.SpringExtensions.Objects.Factory.Config
{
    using System;
    using System.Collections;

    using Spring.Core.TypeResolution;
    using Spring.Objects.Factory.Config;

    /// <summary>
    /// This is the customised Spring.Objects.Config.TypeAliasesConfigurer with the get accessor
    /// for the TypeAliases property.
    /// </summary>
    [Serializable]
    public class TypeAliasConfigurer : AbstractConfigurer
    {
        // Fields
        private IDictionary types;
                
        /// <summary>
        /// Posts the process object factory.
        /// </summary>
        /// <param name="factory">The factory.</param>
        public override void PostProcessObjectFactory(IConfigurableListableObjectFactory factory)
        {
            if (this.types != null)
            {
                foreach (DictionaryEntry entry in this.types)
                {
                    string alias = entry.Key.ToString();
                    Type type = this.ResolveRequiredType(entry.Value, "value", "custom type alias");
                    TypeRegistry.RegisterType(alias, type);
                }
            }
        }

        /// <summary>
        /// Gets or sets the type aliases.
        /// </summary>
        /// <value>The type aliases.</value>
        public IDictionary TypeAliases
        {
            get
            {
                return this.types;
            }

            set
            {
                this.types = value;
            }
        }
    }
}